package com.xms.form.service;

import com.xms.form.core.FormType;
import com.xms.form.core.component.*;
import com.xms.form.entity.SystemForm;
import com.xms.schema.entity.Entity;
import com.xms.utils.JsonUtil;
import com.xms.utils.UUIDUtil;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;

/**
 * <p>
 * DefaultSystemformProvider
 * </p>
 *
 * @author migo
 * @since 2021/9/3
 */
@Service
public class DefaultSystemformProvider implements IDefaultSystemformProvider{

    @Override
    public SystemForm get(Entity entity) {
        SystemForm form = new SystemForm();
        form.setCanbedeleted(false);
        form.setCreatedby(entity.getCreatedby());
        form.setCreatedon(LocalDateTime.now());
        form.setEntityid(entity.getEntityid());
        form.setEntityname(entity.getName());
        form.setFormtype(FormType.Main);
        form.setName(entity.getLocalizedname());
        form.setStatecode(true);
        form.setSystemformid(UUIDUtil.get());
        form.setIsdefault(true);
        form.setIscustomizable(true);
        form.setAuthorizationenabled(false);
        form.setIscustombutton(false);
        form.setOrganizationid(entity.getOrganizationid());

        FormDescriptor formObj = new FormDescriptor();
        formObj.Name = entity.getLocalizedname();
        formObj.IsShowNav = false;
        formObj.Panels = new ArrayList<>();
        PanelDescriptor infoPanel = new PanelDescriptor();
        infoPanel.Name = "基本信息";
        infoPanel.Label = "基本信息";
        infoPanel.IsExpanded = true;
        infoPanel.IsShowLabel = true;
        infoPanel.IsVisible = true;
        infoPanel.Sections = new ArrayList<>();
        SectionDescriptor section = new SectionDescriptor();
        section.IsShowLabel = false;
        section.IsVisible = true;
        section.Label = "基本信息";
        section.Columns = 2;
        section.Rows = new ArrayList<>();
        RowDescriptor row = new RowDescriptor();
        row.IsVisible = true;
        row.Cells = new ArrayList<>();
        CellDescriptor cell = new CellDescriptor();
        cell.IsShowLabel = true;
        cell.IsVisible = true;
        cell.Label = "名称";
        cell.Control = new ControlDescriptor();
        cell.Control.EntityName = entity.getName();
        cell.Control.Name = "name";
        cell.Control.setControlType(FormControlType.Standard);
        row.Cells.add(cell);
        section.Rows.add(row);
        infoPanel.Sections.add(section);
        formObj.Panels.add(infoPanel);
        form.setFormconfig(JsonUtil.toJSONString(formObj));
        return form;
    }
}
